package com.yk.spark.model.app.state.action;

import com.yk.spark.model.SubmitModel;
import org.json.JSONException;
import org.json.JSONObject;

public class AppActionState {
    private String action;
    private String message;
    private String serverSparkVersion;
    private String submissionId;
    private boolean success;

    public AppActionState() {
    }

    public AppActionState(String actionResponse) {

        try {
            JSONObject response=new JSONObject(actionResponse);
            if(response.has("action")
                    &&response.has("message")
                    &&response.has("serverSparkVersion")
                    &&response.has("submissionId")
                    &&response.has("success")){
                this.setAction(response.getString("action"));
                this.setMessage(response.getString("message"));
                this.setServerSparkVersion(response.getString("serverSparkVersion"));
                this.setSubmissionId(response.getString("submissionId"));
                this.setSuccess(response.getBoolean("success"));
            }else{
               throw new NullPointerException();
            }
        } catch (JSONException | NullPointerException e) {
            e.printStackTrace();
        }
    }
    public SubmitModel getModel(){
        SubmitModel model=new SubmitModel();
        model.setSubmissionId(this.getSubmissionId());
        model.setAction(this.getAction());
        model.setMessage(this.getMessage());
        model.setSuccess(this.isSuccess());
        model.setServerSparkVersion(this.getServerSparkVersion());
        return model;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getServerSparkVersion() {
        return serverSparkVersion;
    }

    public void setServerSparkVersion(String serverSparkVersion) {
        this.serverSparkVersion = serverSparkVersion;
    }

    public String getSubmissionId() {
        return submissionId;
    }

    public void setSubmissionId(String submissionId) {
        this.submissionId = submissionId;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "AppActionState{" +
                "action='" + action + '\'' +
                ", message='" + message + '\'' +
                ", serverSparkVersion='" + serverSparkVersion + '\'' +
                ", submissionId='" + submissionId + '\'' +
                ", success=" + success +
                '}';
    }
}
